package com.project.config;

import com.project.handler.ProductHandler;
import com.project.service.CommonChatAssistant;
import com.project.service.NetChatAssistant;
import com.project.service.ProductExtractor;
import com.project.service.ProductManagerAssistant;
import dev.langchain4j.memory.chat.ChatMemoryProvider;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.chat.StreamingChatLanguageModel;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.model.openai.OpenAiEmbeddingModel;
import dev.langchain4j.model.openai.OpenAiStreamingChatModel;
import dev.langchain4j.service.AiServices;
import dev.langchain4j.store.memory.chat.ChatMemoryStore;
import dev.langchain4j.web.search.WebSearchTool;
import dev.langchain4j.web.search.searchapi.SearchApiWebSearchEngine;
import jakarta.annotation.PostConstruct;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.Duration;
import java.util.List;

/**
 * @Author: zhoujinchuan
 * @Description: （）
 * @Date: 2025/2/25 16:38
 */
@Configuration
public class LLMConfig {

    /**
     * 聊天对象
     *
     * @return
     */
    @Bean
    public ChatLanguageModel chatLanguageModel() {
        return OpenAiChatModel.builder()
                .apiKey("sk-6592514f30eb46279298ba765edee4ab")
                .modelName("qwen-turbo")
                .baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1")
                // 启用请求日志
                .logRequests(true)
                // 启用响应日志
                .logResponses(true)
                // 添加自己的事件监听
                .listeners(List.of(new MyChatModelListener()))
                // 配置重试逻辑，指定重试次数和延迟等参数
                .maxRetries(5)
                // 设置10秒超时
                .timeout(Duration.ofSeconds(10))
                .build();
    }


    /**
     * 流式传输聊天对象
     *
     * @return
     */
    @Bean
    public StreamingChatLanguageModel streamingChatLanguageModel() {
        return OpenAiStreamingChatModel.builder()
                .apiKey("sk-6592514f30eb46279298ba765edee4ab")
                .modelName("qwen-turbo")
                .baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1")
                // 启用请求日志
                .logRequests(true)
                // 启用响应日志
                .logResponses(true)
                // 添加自己的事件监听
                .listeners(List.of(new MyChatModelListener()))
                // 设置10秒超时
                .timeout(Duration.ofSeconds(15))
                .build();
    }

    /**
     * 记忆缓存存储对象
     * @return
     */
    @Bean
    public ChatMemoryStore chatMemoryStore() {
        return new PersistentChatMemoryStore();
    }

    /**
     * 多用户或多对话隔离的场景
     * @param chatMemoryStore
     * @return
     */
    @Bean
    public ChatMemoryProvider chatMemoryProvider(ChatMemoryStore chatMemoryStore) {
        // 注意每个memoryId对应创建一个ChatMemory
        return memoryId -> MessageWindowChatMemory.builder()
                .id(memoryId)
                .maxMessages(10)
                .chatMemoryStore(chatMemoryStore)
                .build();
    }

    /**
     * 注入软件产品经理聊天服务对象
     *
     * @param streamingChatLanguageModel
     * @return
     */
    @Bean
    public ProductManagerAssistant productManagerAssistant(StreamingChatLanguageModel streamingChatLanguageModel,
                                                           ChatMemoryProvider chatMemoryProvider) {
        return AiServices.builder(ProductManagerAssistant.class)
                .streamingChatLanguageModel(streamingChatLanguageModel)
                // 记忆缓存
                .chatMemoryProvider(chatMemoryProvider)
                .build();
    }

    /**
     * 注入普通聊天服务对象
     *
     * @param streamingChatLanguageModel
     * @return
     */
    @Bean
    public CommonChatAssistant commonChatAssistant(StreamingChatLanguageModel streamingChatLanguageModel) {
        SearchApiWebSearchEngine searchEngine = SearchApiWebSearchEngine.builder()
                // 测试使用
                .apiKey("p8SZVNAweqTtoZBBTVnXttcj")
                .engine("baidu")
                .build();

        WebSearchTool webSearchTool = WebSearchTool.from(searchEngine);
        return AiServices.builder(CommonChatAssistant.class)
                .streamingChatLanguageModel(streamingChatLanguageModel)
                // 注册工具
                .tools(new ProductHandler())
                .build();
    }

    /**
     * 注入普通联网聊天服务对象
     *
     * @param streamingChatLanguageModel
     * @return
     */
    @Bean
    public NetChatAssistant netChatAssistant(StreamingChatLanguageModel streamingChatLanguageModel) {
        SearchApiWebSearchEngine searchEngine = SearchApiWebSearchEngine.builder()
                // 测试使用
                .apiKey("Rxo9i1YkujMQtRXwWeiv4ckP")
                .engine("google")
                .build();

        WebSearchTool webSearchTool = WebSearchTool.from(searchEngine);
        return AiServices.builder(NetChatAssistant.class)
                .streamingChatLanguageModel(streamingChatLanguageModel)
                // 注册工具
                .tools(new ProductHandler(),webSearchTool)
                .build();
    }

    /**
     * 注入产品提取，以及问预算对象
     *
     * @param chatLanguageModel
     * @return
     */
    @Bean
    public ProductExtractor productDemandExtractor(ChatLanguageModel chatLanguageModel) {
        return AiServices.builder(ProductExtractor.class)
                .chatLanguageModel(chatLanguageModel)
                .build();
    }


}
